<?php
/**
 *@Author:		Ian-QWL
 *@Function:	基础共享类
 */
!defined('IAN') && exit('Access Denied');
  
class Common
{
	/* GetPDO */
	public function getPDO()
	{
		require(CONFS.DS.'db.conf.php');
		require(MODELS.DS.'pdo.cs.php');
		return $pdo;
	}
	/* GetSmarty */	
	public function getSmarty()
	{
		require_once(CONFS.DS.'smarty.conf.php');
		return $smarty;
	}
	/* 检查文件权限 */
	function checkMode($name)
	{
		
	}

	/* 过滤敏感词语 */
	function filtrateWord($str)
	{
		include '../configs/user.conf.php';
		if(in_array($str,$banUid))
		{
			echo 'bad uid';
		}
		else
		{
			echo 'goog uid';	
		}		
	}
	
	/*获取字符个数
	 *UTF-8 编码下中文占三个字符
	 *转换后中文字符占用两个字符 
	 */
	function str_len($str)
	{
		$length = strlen(preg_replace('/[\x00-\x7F]/', '', $str));
		if($length)
		{
			return strlen($str) - $length + intval($length / 3) * 2;
		}
		else
		{
			return strlen($str);
		}
	}
	
	/* 递归转义字符中的特殊字符 */
	function stripslashes_deep($value)
	{
		if (empty($value))
		{
			return $value;
		}
		else
		{
			return is_array($value)? array_map('stripslashes_deep', $value): stripslashes($value);
		}
	}
	
	/* 创建指定目录 */
	function makeDir($folder)
	{
		
	}
	
	/* 上传文件位置 */
	function moveUploadFile($file,$target='')
	{
		if (move_uploaded_file($file, $target))
		{
			@chmod($target,0755);
			return true;
		}
		else if (copy($file, $target))
		{
			@chmod($target,0755);
			return true;
		}
		return false;	
	}
	
	/* 检查文件类型 */
	function checkType($file,&$fileType)
	{
		$type = strtolower(array_pop(explode('.', $file)));	
		if(in_array($type,$fileType))
		{
			return true;		
		}
		else
		{	
			return false;		
		}
	}
	
	/* 正则GET请求 */
	function checkGet($_GET)
	{
	
	}
	/* 检查-CID */
	function checkCID($cid)
	{
	
	}
	
	/* 检查-Email */
	function checkEmail($email)
	{
		$result = preg_match('/^[a-z][a-z_0-9\.-]*[a-z0-9]+@[a-z0-9]+.[a-z]+$/i',$email);	
		if($result)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	/* 检查-PWD */
	function subPWD($pwd)
	{
		return substr(md5($pwd),8,16);
	}
	
	/* ClientIP */
	function getCIP()
	{
		static $cip = NULL;
		if ($cip !== NULL)
		{
			return $cip;
		}
		/* 判断是否通过代理上网 */
		if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
		{
			$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
			/* 获取IP数组中第一个非unknown的有效IP字符串 */
			foreach ($arr AS $ip)
			{
				$ip = trim($ip);
				if ($ip != 'unknown')
				{
					$cip = $ip;
					break;
				}
			}
		}
		elseif (isset($_SERVER['HTTP_CLIENT_IP']))
		{
			$cip = $_SERVER['HTTP_CLIENT_IP'];
		}
		else
		{
			if (isset($_SERVER['REMOTE_ADDR']))
			{
				$cip = $_SERVER['REMOTE_ADDR'];
			}
			else
			{
				$cip = '0.0.0.0';
			}
		}
		preg_match("/[\d\.]{7,15}/", $cip, $userip);
		$cip = !empty($userip[0]) ? $userip[0] : '0.0.0.0';
		return $cip;
	}
	
	/* ServerIP */
	public function getSIP()
	{
		static $sip = NULL;
		if ($sip !== NULL)
		{
			return $sip;
		}
		if (isset($_SERVER['SERVER_ADDR']))
		{
			$sip = $_SERVER['SERVER_ADDR'];
		}
		else
		{
			$sip = '0.0.0.0';
		}
		return $sip;
	}
	
	/* SetCookie */
	function setClientCookie()
	{
		setcookie($key, $value, 0, '/', '.ian.com', 0);
	}
}

?>